package com.example.a26436.newpractise.practise2;


import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.example.a26436.newpractise.R;

public class ContactActivity extends AppCompatActivity {

    private static final String TAG = "ContactActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);

        int permission = ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_CONTACTS);
        if (permission == PackageManager.PERMISSION_GRANTED){
            obtainContacts();
        }else{
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{Manifest.permission.READ_CONTACTS},
        1000);
        }
    }

    @Override
    public void onRequestPermissionsResult(
            int requestCode,
            @NonNull String[] permissions,
            @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 1000:
                if (grantResults[0] ==PackageManager.PERMISSION_GRANTED){
                    obtainContacts();
                }
                break;
        default:
            Log.e(TAG, "onRequestPermissionsResult: "+
                    "I don't know this request code!please check!" );
        break;
        }
    }

    private void obtainContacts() {
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(
                ContactsContract.Contacts.CONTENT_URI,
                null,
                null,
                null,
                null);
        while(cursor.moveToNext()) {
            String name = cursor.getString(
                    cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String _id = cursor.getString(
                    cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String selections = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?";
            Cursor cursor1 = contentResolver.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    selections,
                    new String[]{_id},
                    null
            );
            while (cursor1.moveToNext()){
                String phone = cursor1.getString(
                        cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                name = name +"----"+ phone;
            }
            Log.e(TAG, "obtainContacts: "+name );

        }
    }
}
